<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Content for Article->Category
 * set content in category.tpl.php for category browse
 * set content in category.editor.tpl.php for category editor
 * include functions to use in category pages
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
function contentPage($refPage, $page, $subpage, $refValidate)
{
	// This if-loop get the code of sub-codes for delete, change order an activate field
	// and set the 'Output'page as category-table or category-editor
	$functions = 'module/function/article.func.php';
	if (is_file($functions))
		include_once $functions;

	$refPage->assign('categoryPageHelp', help('categoryPage', $page, TRUE));

	if (isset($_GET['active']))
	{  // activate or deactivate category
		$pagefile = 'module/page/article/category.page.action.php';
		if (is_file($pagefile))
			include_once $pagefile;

		$pagefile = 'module/page/article/category.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
	else if (isset($_GET['order']))
	{  // push up position of selected data record
		$pagefile = 'module/page/article/category.page.order.php';
		if (is_file($pagefile))
			include_once $pagefile;

		$pagefile = 'module/page/article/category.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
	else if (isset($_GET['func']))
	{
		if ($refValidate->cleanslash($_GET['func']) === 'edit')
		{ // call the editor page of selectet category
			$pagefile = 'module/page/article/category.page.editor.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_GET['func']) === 'pagi')
		{ // Display new page
			$number = intval($_GET['nr']);
			$_SESSION['pageC'] = $number;

			$pagefile = 'module/page/article/category.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('New'))
		{ // create a new category
			$data = array('category_name' => 'New');
			db::getInstance()->insert('category', $data);
			$result = db::getInstance()->rawQuery("SELECT LAST_INSERT_ID()");
			$_GET['id'] = intval($result[0]['LAST_INSERT_ID()']);
			db::getInstance()->where("category_id", $_GET['id']);
			$result = db::getInstance()->getOne("category");
			$catTopy = intval($result['category_top']);
			$result = db::getInstance()->rawQuery("SELECT count(*) FROM category " .
				"WHERE category_top ='" . $catTopy . "'");
			$count = intval($result[0]['count(*)']);
			$data = array('category_order' => $count, 
			 'category_create' => date("Y-m-d H:i:s"));
			db::getInstance()->where('category_id', $_GET['id']);
			db::getInstance()->update('category', $data);

			// call the editor page for the new category
			$pagefile = 'module/page/article/category.page.editor.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Abort'))
		{
			$pagefile = 'module/page/article/category.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Copy'))
		{ // Copy marked category 1:1
			if (!empty($_POST['marker']))
			{
				// copy category
				$pagefile = 'module/page/article/category.page.copy.php';
				if (is_file($pagefile))
					include_once $pagefile;
			}

			$pagefile = 'module/page/article/category.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Save'))
		{ // save data from editorpage and then call categorypage
			$pagefile = 'module/page/article/category.page.save.php';
			if (is_file($pagefile))
				include_once $pagefile;

			$pagefile = 'module/page/article/category.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Refresh'))
		{ // save data from editorpage and then call editorpage
			$pagefile = 'module/page/article/category.page.save.php';
			if (is_file($pagefile))
				include_once $pagefile;

			$pagefile = 'module/page/article/category.page.editor.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Delete'))
		{ // open the "Delete"-Window
			if (!empty($_POST['marker']))
			{
				$pagefile = 'module/page/article/category.page.delete.php';
			}
			else
			{
				$pagefile = 'module/page/article/category.page.table.php';
			}

			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_GET['func']) === 'deleteNow')
		{ // copy Category to basket and delete the source
			$deleteIds = explode('-', $refValidate->cleanslash($_POST['deleteId']));

			foreach ($deleteIds as $value)
			{
				db::getInstance()->where("category_id", $value);

				if ($cat = db::getInstance()->getOne("category"))
				{
					$catTop = $cat['category_top'];
					db::getInstance()->rawQuery("INSERT INTO category_basket SELECT * FROM category " .
						"WHERE category.category_id = '" . $value . "'");
					db::getInstance()->where("category_id", $value);

					if (db::getInstance()->delete('category'))
					{
						message::setNoteRow($cat['category_id'] . ' ' . $cat['category_name'] . ' -> ' .
							_('Data were moved into the basket.'));
					}
					else
					{
						message::setErrorRow($cat['category_id'] . ' ' . $cat['category_name'] . ' -> ' .
							_('Faults delete the data - move/delete.'));
					}

					$ordernr = 1;
					if ($categoryx = db::getInstance()->rawQuery("SELECT category_id FROM category WHERE " .
						"category_top ='" . $catTop . "' ORDER BY category_order"))
					{
						foreach ($categoryx as $valuex)
						{
							$data = Array('category_order' => $ordernr);
							db::getInstance()->where('category_id', $valuex['category_id']);
							db::getInstance()->update('category', $data);
							$ordernr++;
						}
					}

					if (!empty($cat['category_small_image']))
					{ // Move small image in basket-path
						if (!is_dir('../images/basket/' . CATEGORY_IMAGE_PATH))
						{
							mkdir('../images/basket/' . CATEGORY_IMAGE_PATH);
						}
						if (!is_dir('../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value))
						{
							mkdir('../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value);
						}
						if (!is_dir('../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value . '/' .
								CATEGORY_IMAGE_THUMB_PATH))
						{
							mkdir('../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value . '/' .
								CATEGORY_IMAGE_THUMB_PATH);
						}

						if (rename('../images/' . CATEGORY_IMAGE_PATH . '/' . $value . '/' .
								CATEGORY_IMAGE_THUMB_PATH . '/' . $cat['category_small_image'],
								'../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value . '/' .
								CATEGORY_IMAGE_THUMB_PATH . '/' . $cat['category_small_image']))
						{
							rmdir('../images/' . CATEGORY_IMAGE_PATH . '/' . $value . '/' .
								CATEGORY_IMAGE_THUMB_PATH);
							if (empty($cat['category_image']))
								rmdir('../images/' . CATEGORY_IMAGE_PATH . '/' . $value);
							message::setNoteRow($cat['category_id'] . ' ' . $cat['category_name'] . ' -> ' .
								_('The old small image file was moved successfully. '));
						}
					}

					if (!empty($cat['category_image']))
					{ // Move image in basket-path
						if (!is_dir('../images/basket/' . CATEGORY_IMAGE_PATH))
						{
							mkdir('../images/basket/' . CATEGORY_IMAGE_PATH);
						}
						if (!is_dir('../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value))
						{
							mkdir('../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value);
						}

						if (rename('../images/' . CATEGORY_IMAGE_PATH . '/' . $value . '/' .
								$cat['category_image'],
								'../images/basket/' . CATEGORY_IMAGE_PATH . '/' . $value . '/' .
								$cat['category_image']))
						{
							rmdir('../images/' . CATEGORY_IMAGE_PATH . '/' . $value);
							message::setNoteRow($cat['category_id'] . ' ' . $cat['category_name'] . ' -> ' .
								_('The old image file was moved successfully. '));
						}
					}
				}
			}
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Next Page'))
		{
			$_SESSION['pageC'] ++;

			$pagefile = 'module/page/article/category.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
		else if ($refValidate->cleanslash($_REQUEST['submit']) === _('Previous Page'))
		{
			$_SESSION['pageC'] --;
			
			$pagefile = 'module/page/article/category.page.table.php';
			if (is_file($pagefile))
				include_once $pagefile;
		}
	}
	else
	{  // get the category-table
		$pagefile = 'module/page/article/category.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
}

/**
 * New numbering of the source category (after delete/move) and
 * new order number for the target category (new or moved categorys be appended to it)
 *
 * @param  string $newCategory	Target category-id
 * @param  string $oldCategory	Erorcode for output
 * @return new order-value as numeric
 */
function sortOrder($newCategoryTopx, $oldCategoryTopx, $idx)
{
	$resultx = db::getInstance()->rawQuery("SELECT category_id, category_order, " .
		"category_top FROM category WHERE category_top = '" . $oldCategoryTopx . "'" .
		" ORDER BY category_order");

	$orderCount = 0;

	if (count($resultx) > 1)
	{
		foreach ($resultx as $dbRow)
		{
			$orderCount++;

			if ($dbRow['category_id'] == $idx)
			{
				$orderCount--;
			}

			$data = Array('category_order' => $orderCount);
			db::getInstance()->where('category_id', $dbRow['category_id']);
			db::getInstance()->update('category', $data);
		}
	}
	db::getInstance()->where('category_top', $newCategoryTopx);
	$result = db::getInstance()->get('category');
	$orderCount = count($result) + 1;
	return $orderCount;
}

?>